/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundaries;

import controllers.SearchManager;
import entities.User;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Alberto, Alessandra
 */
@ManagedBean
@ViewScoped
public class SearchBean {
    
    @EJB
    SearchManager sm;
    
    private String username;
    
    private List<User> users;
     
    private User selectedUser;

    public SearchBean() {
        this.users = new ArrayList<>();
        this.username = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("search");
    }
    
    @PostConstruct
    public void init() {
        users.addAll(sm.search(username));
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public User getSelectedUser() {
        return selectedUser;
    }

    public void setSelectedUser(User selectedUser) {
        this.selectedUser = selectedUser;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    
    public void search () {
        users.clear();
        users.addAll(sm.search(username));
    }
    
    public String viewProfile () {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selecteduser", selectedUser);
        return "userprofile?faces-redirect=true";
    }
    
}
